const Trie = require("../template")

function testTrieBasic() {
  const trie = new Trie()

  console.log(
    "1. 基本插入和搜索测试:",
    (() => {
      trie.insert("hello")
      trie.insert("world")
      trie.insert("hi")
      return trie.search("hello") === true &&
             trie.search("world") === true &&
             trie.search("hi") === true &&
             trie.search("hell") === false &&
             trie.search("xyz") === false
    })()
  )

  console.log(
    "2. 前缀匹配测试:",
    (() => {
      trie.insert("cat")
      trie.insert("car")
      trie.insert("card")
      trie.insert("care")
      return trie.startsWith("ca") === true &&
             trie.startsWith("car") === true &&
             trie.startsWith("card") === true &&
             trie.startsWith("xyz") === false
    })()
  )

  console.log(
    "3. 空字符串处理测试:",
    (() => {
      const emptyTrie = new Trie()
      emptyTrie.insert("")
      return emptyTrie.search("") === true &&
             emptyTrie.startsWith("") === true
    })()
  )

  console.log(
    "4. 单字符测试:",
    (() => {
      const singleTrie = new Trie()
      singleTrie.insert("a")
      singleTrie.insert("b")
      return singleTrie.search("a") === true &&
             singleTrie.search("b") === true &&
             singleTrie.search("c") === false &&
             singleTrie.startsWith("a") === true
    })()
  )

  console.log(
    "5. 重复插入测试:",
    (() => {
      const repeatTrie = new Trie()
      repeatTrie.insert("test")
      repeatTrie.insert("test")
      repeatTrie.insert("test")
      return repeatTrie.search("test") === true &&
             repeatTrie.startsWith("te") === true
    })()
  )

  console.log(
    "6. 嵌套单词测试:",
    (() => {
      const nestedTrie = new Trie()
      nestedTrie.insert("cat")
      nestedTrie.insert("category")
      nestedTrie.insert("categorize")
      return nestedTrie.search("cat") === true &&
             nestedTrie.search("category") === true &&
             nestedTrie.search("categorize") === true &&
             nestedTrie.startsWith("cat") === true
    })()
  )

  console.log(
    "7. this上下文Trie测试:",
    (() => {
      const trieObj = {
        multiplier: 2,
        processTrie: function(words) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          return trie.search("test") ? this.multiplier : 0
        }
      }
      return trieObj.processTrie(["test", "hello"]) === 2
    })()
  )

  console.log(
    "8. 复杂对象this绑定Trie测试:",
    (() => {
      const trieConfigObj = {
        config: { prefix: "word_" },
        processWithConfig: function(words) {
          const trie = new Trie()
          words.forEach(word => trie.insert(this.config.prefix + word))
          return trie.search("word_test")
        }
      }
      return trieConfigObj.processWithConfig(["test", "hello"]) === true
    })()
  )

  console.log(
    "9. Trie结构一致性测试:",
    (() => {
      const consistencyTrie = new Trie()
      consistencyTrie.insert("hello")
      consistencyTrie.insert("world")
      const searchResult = consistencyTrie.search("hello")
      const prefixResult = consistencyTrie.startsWith("hel")
      return searchResult === true && prefixResult === true
    })()
  )

  console.log(
    "10. Trie边界测试:",
    (() => {
      const boundaryTrie = new Trie()
      boundaryTrie.insert("a")
      boundaryTrie.insert("ab")
      boundaryTrie.insert("abc")
      return boundaryTrie.search("a") === true &&
             boundaryTrie.search("ab") === true &&
             boundaryTrie.search("abc") === true &&
             boundaryTrie.search("abcd") === false &&
             boundaryTrie.startsWith("a") === true &&
             boundaryTrie.startsWith("ab") === true &&
             boundaryTrie.startsWith("abc") === true &&
             boundaryTrie.startsWith("abcd") === false
    })()
  )
}

testTrieBasic()